package com.tsinghuabigdata.edu.ddmath.requestHandler.requestImpl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tsinghuabigdata.edu.commons.http.HttpRequest;
import com.tsinghuabigdata.edu.commons.http.HttpRequestException;
import com.tsinghuabigdata.edu.ddmath.bean.JoinedClassInfo;
import com.tsinghuabigdata.edu.ddmath.bean.LoginInfo;
import com.tsinghuabigdata.edu.ddmath.bean.ResultInfo;
import com.tsinghuabigdata.edu.ddmath.bean.School;
import com.tsinghuabigdata.edu.ddmath.bean.UserDetailinfo;
import com.tsinghuabigdata.edu.ddmath.commons.http.AppRequest;
import com.tsinghuabigdata.edu.ddmath.commons.http.AppRequestUtils;
import com.tsinghuabigdata.edu.ddmath.constant.AppRequestConst;
import com.tsinghuabigdata.edu.ddmath.module.apkupgrade.UpdateInfo;
import com.tsinghuabigdata.edu.ddmath.requestHandler.LoginReqService;
import com.tsinghuabigdata.edu.ddmath.util.AppLog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by 28205 on 2016/12/14.
 */
public class LoginReqImpl extends BaseService implements LoginReqService {

    @Override
    public LoginInfo stulogin(String loginName, String password, String deviceId) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.POST_LOGIN_STUDENT);
        HttpRequest request = AppRequestUtils.post(url);
        request.putRequestParam("loginName", loginName)
                .putRequestParam("password", password)
                .putRequestParam("deviceId", deviceId)
                .putRequestParam("clientType", "android")
                .request().getBody();
        LoginInfo loginInfo = request.getResult(LoginInfo.class);

        //
        url = getUrl(AppRequestConst.LOGIN_ACCESS);
        request = AppRequestUtils.get(url);
        request.putHeader("access_token", loginInfo.getAccessToken());
        String body = request.requestJson().getBody();
        AppLog.i(" login_access body = " + body);
        JSONObject json = new JSONObject(body);
        if (!json.has("domain")) throw new HttpRequestException("图片服务器地址失败", request);
        String domain = json.getString("domain");
        if (!domain.startsWith("http:")) {
            domain = "http://" + domain;
        }
        loginInfo.setFileServer(domain);
        return loginInfo;
    }

    @Override
    public void stulogout(String accessToken) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.POST_LOGOUT_STUDENT);
        HttpRequest request = AppRequestUtils.post(url);
        request.putHeader("access_token", accessToken)
                .requestJson();
        return;
    }

    @Override
    public void register(String nickName, String cellPhoneNumber, String password, String gender, String verifyCode) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.POST_REGISTER);
        HttpRequest request = AppRequestUtils.post(url);
        request.putRequestParam("nickName", nickName)
                .putRequestParam("cellPhoneNumber", cellPhoneNumber)
                .putRequestParam("password", password)
                .putRequestParam("sex", gender)
                .putRequestParam("verifyCode", verifyCode)
                .request();
        String body = request.getBody();
        return;
    }

    @Override
    public void updateExtraPersoninfo(String access_token, String accountId, String reallyName, String enrollmentYear) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.POST_UPDATE_EXTRA_PERSONINFO);
        HttpRequest request = AppRequestUtils.post(url);
        request.putHeader("access_token", access_token)
                .putRequestParam("accountId", accountId)
                .putRequestParam("reallyName", reallyName)
                .putRequestParam("enrollmentYear", enrollmentYear)
                .request().getBody();
    }

    @Override
    public List<School> queryBlurSchool(String schoolName) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.GET_QUERY_BLURSCHOOL);
        HttpRequest request = AppRequestUtils.get(url);
        request.putRequestParam("schoolName", schoolName)
                .request().getBody();
        return request.getResult(new TypeToken<List<School>>() {
        });
    }

    @Override
    public void getVeryfyCode(String phone, String type) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.GET_GET_VERIFYCODE);
        HttpRequest request = AppRequestUtils.get(url);
        request.putRequestParam("phone", phone)
                .putRequestParam("type", type)
                .request().getBody();

        return;
    }

    @Override
    public void resetPass(String phone, String captcha, String newPassword, String confirmPassword) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.POST_RESET_PASS);
        HttpRequest request = AppRequestUtils.post(url);
        request.putRequestParam("phone", phone)
                .putRequestParam("captcha", captcha)
                .putRequestParam("newPassword", newPassword)
                .putRequestParam("confirmPassword", confirmPassword)
                .request();
        request.getBody();
        return;
    }

    @Override
    public List<JoinedClassInfo> getJoinedClassList(String accessToken, String studentId) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.GET_GET_CLASSLIST);
        HttpRequest request = AppRequestUtils.get(url);
        request.putHeader("access_token", accessToken)
                .putRestfulParam("studentId", studentId)
                .request().getBody();
        return request.getResult(new TypeToken<List<JoinedClassInfo>>() {
        });
    }

    @Override
    public ResultInfo isPhoneUsed(String phone) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.GET_PHONENUM_ISUSED);
        HttpRequest request = AppRequestUtils.get(url);
        request.putRequestParam("phone", phone)
                .request();
        return request.getResult(ResultInfo.class);
    }

    @Override
    public UserDetailinfo queryUserDetailinfo(String accessToken, String accountId) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.GET_USER_DETAILINFO);
        HttpRequest request = AppRequestUtils.get(url);
        request.putHeader("access_token", accessToken)
                .putRestfulParam("accountId", accountId)
                .request().getBody();
        return request.getResult(UserDetailinfo.class);
    }

    @Override
    public UpdateInfo queryApkUpdateinfo() throws HttpRequestException, JSONException {
        String url = getUpgradeApkUrl(AppRequestConst.GET_APK_UPDGRADE);
        AppRequest request = (AppRequest) AppRequestUtils.get(url);
        request.request();
        String body =  request.getFullBody();
        return new Gson().fromJson(body, UpdateInfo.class);
    }

    @Override
    public Object bindMobile(String access_token, String accountId, String cellphone, String verifycode) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.POST_BINDMOBILE);
        HttpRequest request = AppRequestUtils.post(url);
        request.putHeader("access_token", access_token)
                .putRequestParam("accountId", accountId)
                .putRequestParam("cellphone", cellphone)
                .putRequestParam("verifyCode", verifycode)
                .request();
        return request.getBody();
    }
}
